package 抽象数据类型;

import java.util.Iterator;

public class Queue<Item> implements Iterable<Item> {
    private Node first; //最早添加的元素
    private Node last; //最近添加的元素
    private int N;

    @Override
    public Iterator<Item> iterator() {
        return new  LinkIterator();
    }
    public class LinkIterator implements Iterator<Item> {
        Node current=first;
        @Override
        public boolean hasNext() {
            return current!=null;
        }

        @Override
        public Item next() {
            Node next = current;
            current = current.next;
            return next.item;
        }
    }

    private class Node{
        Item item;
        Node next;
    }

    public boolean isEmpty() {
        return first == null;
    }

    public int Size() {
        return N;
    }

    public void enqueue(Item item) {

        Node oldlast = last;
        last = new Node();
        last.item = item;
        last.next = null;
        if (isEmpty()) {
            first = last;
        } else {
            oldlast.next = last;
        }

        N++;
    }

    public Item dequeue() {
        Item item = first.item;
        first = first.next;
        if (isEmpty()) {
            last = null;
        }
        N--;
        return item;
    }


}
